package com.xianda.selenium;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Navigation;

import com.xianda.browser.Firefox;

/**
 * 简介：WebElement对象提供的各种定位元素策略 <br/>
 * ID： driver.findElement(By.id(<elementID>))<br/>
 * Name： driver.findElement(By.name(<elementName>))<br/>
 * className： driver.findElement(By.className(<elementClassName>))<br/>
 * tagName： driver.findElement(By.tagName(<htmlTagName>))<br/>
 * linkText： driver.findElement(By.linkText(<linkText>))<br/>
 * css： driver.findElement(By.cssSelector(<cssSelector>))<br/>
 * xpath： driver.findElement(By.xpath(<xpathQuery>))<br/>
 * partialLinkText：
 * driver.findElement(By.partialLinkText(<partialLinkText>))<br/>
 * 
 * @author xianda
 *
 */
public class Main {
	public static void main(String[] args) throws IOException {
		// 浏览器选择择
		WebDriver driver = new Firefox().getDriver();
		// 导航至需要的页面
		Navigation navigation = driver.navigate();
		// navigation.to("http://172.19.60.175:8002/bha-xm-boss/login/show.html");
		navigation.to("http://172.19.60.175:8002/bha-xm-boss/login/show.html");
		// 获取输入框
		// WebElement element = driver.findElement(By.id("dropbox"));
		// // 获取按钮
		// WebElement saveButton = driver.findElement(By.id("su"));
		// // 点击按钮File file = new File("C:\\Users\\happy\\Desktop\\FileUpload.html");
		// 用java来实现文件读取功 \
		// saveButton.click();

		// js.executeScript("alert('Test Case Execution Is started Now..');");
		// File file = new File("C:\\Users\\xianda\\Desktop\\dev-test");
		// driver.get(file.getAbsolutePath());
		String string = read("C:\\Users\\xianda\\Desktop\\dev-test");
		JavascriptExecutor js = (JavascriptExecutor) driver;
		js.executeScript("dropKey(new File([\"" + string + "\"],\"test\",{type: \"text/plain\"}))");

		// 用java来实现文件读取功 \
		// driver.get(file.getAbsolutePath());
		// driver.findElement(By.name("dropbox")).sendKeys("http://172.19.60.175:8002/bha-xm-boss/login/show.html");

		// // 获得cookie
		// Set<Cookie> coo = driver.manage().getCookies();
		// System.out.println(coo);
		// //删除所有的cookie
		// // driver.manage().deleteAllCookies();
		// driver.close();

	}

	// public static void main(String[] args) throws IOException {
	// String string = read("C:\\Users\\xianda\\Desktop\\dev-test");
	// System.out.println(string);
	// }

	private static String read(String path) throws IOException {
		File file = new File(path);
		BufferedReader reader = null;
		String body = "";
		try {
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;
			while ((tempString = reader.readLine()) != null) {
				body += tempString;
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}
		return body;
	}

}